﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Joystick : MonoBehaviour
{
    public float maxMove;
    public float value;
    float targetPx;
   
    public void StartMove()
    {
        StartCoroutine(Move());
    }

    public IEnumerator Move()
    {
        for (; ; )
        {
            if (!Input.GetMouseButtonUp(0))
            {
                
                targetPx += MousePosition.mouseMove.x;
                if (Math.Abs(targetPx) < maxMove)
                    ((RectTransform)transform).anchoredPosition = new Vector2(targetPx, 0);
                else
                {
                    if(targetPx>0)
                        ((RectTransform)transform).anchoredPosition = new Vector2(maxMove, 0);
                    else
                        ((RectTransform)transform).anchoredPosition = new Vector2(-maxMove, 0);
                }
                value = transform.localPosition.x / maxMove;
            }
            else
            {
                ((RectTransform)transform).localPosition = Vector3.zero;
                value = 0;
                targetPx = 0;
                break;
            }
            yield return null;
        }
    }
}
